package com.xucy.springboot.activemq;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author xucy
 * @Date 2019-07-05 14:28
 * @Description
 **/

@RestController
@RequestMapping("/myTopicProducer")
public class MyTopicProducer  {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/sendString")
    public String sendString(){

        String topic="myTopic";
        String msg="我的topic";
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topic),msg);

        return "发送成功";
    }

    @RequestMapping("/sendListString")
    public String sendListString(){
        String topic ="myTopicList";
        List<String> stringList=new ArrayList<>();
        stringList.add("xu");
        stringList.add("chen");
        stringList.add("yang");
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topic),stringList);

        return "发送成功";
    }

    @RequestMapping("/sendObject")
    public String sendObject(){
        String topic="myTopicObject";
        User user =new User("老虎",18);
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topic),user);

        return "发送成功";
    }

    @RequestMapping("/sendListObject")
    public String sendListObject(){

        String topic="myTopicListObject";
        List list=new ArrayList();
        list.add(new User("老鼠",10));
        list.add(new User("羊",11));
        list.add(new User("猪",20));
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topic),list);
        return "发送成功";
    }
}
